package com.action;	
	
import java.util.List;	
import javax.servlet.http.HttpServletRequest;	
import org.junit.runners.Parameterized.Parameters;	
import org.springframework.beans.factory.annotation.Autowired;	
import org.springframework.stereotype.Controller;	
import org.springframework.web.bind.annotation.RequestMapping;	
import org.springframework.web.bind.annotation.RequestParam;	
import com.service.ChukuService;	
import com.service.GoodsService;
import com.vo.Chuku;	
import com.vo.Goods;
import com.vo.Ruku;
	
 	
@Controller	
@RequestMapping("/chuku")	
public class ChukuController {	
	
	@Autowired	
	private ChukuService chukuService;	
	
	@Autowired	
	private GoodsService goodsService;
		
	//查询	
	@RequestMapping(value="/list")	
	public String list(Chuku chuku,HttpServletRequest request) throws Exception{	
			
		List<Chuku> list=chukuService.findChukuByParam(chuku);	
		request.setAttribute("list", list);	
		return  "/admin/chuku/list.jsp";	
	}	
		
	//添加	
	@RequestMapping(value="/toadd")	
	public String toadd(Chuku chuku,HttpServletRequest request) throws Exception{	
		this.initGoodsList(request);
		return  "/admin/chuku/add.jsp";	
	}
	
	//添加	
	@RequestMapping(value="/add")	
	public String add(Chuku chuku,HttpServletRequest request) throws Exception{	
		chukuService.insertChuku(chuku);
		int num=chuku.getNum();
		Goods goods=new Goods();
		goods.setName(chuku.getName());
		Goods g=goodsService.findGoodsByParam(goods).get(0);
		g.setNum(g.getNum()-num);
		goodsService.updateGoods(g);
		return  this.list(new Chuku(),request);	
	}	
		
	//进入编辑	
	@RequestMapping(value="/toupdate")	
	public String toupdate(HttpServletRequest request) throws Exception{	
		String id=request.getParameter("id");	
		Chuku chuku=chukuService.findChukubyId(Integer.parseInt(id));	
		request.setAttribute("chuku", chuku);	
		return  "/admin/chuku/update.jsp";	
	}	
		
	//更新	
	@RequestMapping(value="/update")	
	public String update(Chuku chuku,HttpServletRequest request) throws Exception{	
		chukuService.updateChuku(chuku);	
		return  this.list(new Chuku(),request);	
	}	
		
	//删除	
	@RequestMapping(value="/del")	
	public String del(HttpServletRequest request) throws Exception{	
		String id=request.getParameter("id");	
		chukuService.deletChuku(Integer.parseInt(id));	
		return  this.list(new Chuku(),request);	
	}	
		
	//查看	
	@RequestMapping(value="/view")	
	public String view(HttpServletRequest request) throws Exception{	
		String id=request.getParameter("id");	
		Chuku chuku=chukuService.findChukubyId(Integer.parseInt(id));	
		request.setAttribute("chuku", chuku);	
		return  "/admin/chuku/view.jsp";	
	}	
	
	public void initGoodsList(HttpServletRequest request) throws Exception{
		List<Goods> list=goodsService.findGoodsList();
		request.setAttribute("goodslist", list);
	}
}	
